package hu.fic.weddingstorefinder.db;

import hu.fic.weddingstorefinder.db.table.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class WeddingStoreDBAdapter {
	private static final String LOG_TAG = "WeddingStoreDBAdapter";

	private static final String DATABASE_NAME = "weddingstore.db";
	private static final int DATABASE_VERSION = 2;

	private SQLiteDatabase db;
	private final Context context;
	private WeddingStoreDBOpenHelper dbHelper;
	private static WeddingStoreDBAdapter instance;
	
	//singleton methods:
	public static void createInstance(Context _context) {
		instance = new WeddingStoreDBAdapter(_context);
		instance.open();
	}
	
	public static void closeDB() {
		instance.close();
	}
	
	public static WeddingStoreDBAdapter getInstance() throws Exception{
		if (instance == null) {
			throw new Exception("Please call  WeddingStoreDBAdapter.createInstance(Context _context) first");
		} else {
			if (!instance.getDb().isOpen()) {
				instance.open();
			}
			return instance;
		}
	}
	

	public WeddingStoreDBAdapter(Context _context) {
		this.context = _context;
		dbHelper = new WeddingStoreDBOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public void close() {
		db.close();
	}

	public void open() throws SQLiteException {
		try {
			db = dbHelper.getWritableDatabase();
		} catch(SQLiteException ex) {
		db = dbHelper.getReadableDatabase();
	}
	}

	public SQLiteDatabase getDb() {
		return db;
	}
	private static class WeddingStoreDBOpenHelper extends SQLiteOpenHelper {

		public WeddingStoreDBOpenHelper(Context context, String name, CursorFactory factory, int version) {
			super(context, name, factory, version);		}

		// SQL Statement to create a new database.
		private static final String APPLICATIONCONF_TABLE_CREATE = "CREATE TABLE `ApplicationConf` (`AppConfId` INTEGER PRIMARY KEY AUTOINCREMENT, `Name` VARCHAR, `Value` VARCHAR )";
		private static final String POI_TABLE_CREATE = "CREATE TABLE `POI` (`POIId` INTEGER PRIMARY KEY AUTOINCREMENT, `CustomPOIId` INTEGER, `Longitude` DOUBLE PRECISION, `Latitude` DOUBLE PRECISION, `Address` VARCHAR, `City` VARCHAR, `Phone` VARCHAR, `POIName` VARCHAR, `ZIPCode` VARCHAR, `POIStatus` INTEGER )";
		private static final String DRESS_TABLE_CREATE = "CREATE TABLE `Dress` (`DressID` INTEGER PRIMARY KEY AUTOINCREMENT, `CustomDressId` INTEGER, `DressDesc` VARCHAR, `Currency` VARCHAR, `Price` INTEGER, `ImageName` VARCHAR, `CustomPOIId` INTEGER, `DressStatus` INTEGER, `IsAvailable` INTEGER )";
		
		@Override
		public void onCreate(SQLiteDatabase _db) {
			_db.execSQL(APPLICATIONCONF_TABLE_CREATE);
			_db.execSQL(POI_TABLE_CREATE);
			_db.execSQL(DRESS_TABLE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
			Log.w(LOG_TAG, "Upgrading from version " + _oldVersion + " to " + _newVersion + ", which will destroy all old data");

			// Drop the old tables.
			_db.execSQL("DROP TABLE IF EXISTS " + ApplicationConf.TAB_NAME);
			_db.execSQL("DROP TABLE IF EXISTS " + POI.TAB_NAME);
			_db.execSQL("DROP TABLE IF EXISTS " + Dress.TAB_NAME);

			// Create the new ones.
			onCreate(_db);
		}
	}
}